package io.coderyeah.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.coderyeah.basic.Result;
import io.coderyeah.basic.service.BaiduAiAuditService;
import io.coderyeah.basic.util.LoginInfoHolder;
import io.coderyeah.org.domain.Employee;
import io.coderyeah.org.mapper.EmployeeMapper;
import io.coderyeah.pet.domain.Pet;
import io.coderyeah.pet.domain.PetDetail;
import io.coderyeah.pet.domain.PetOnlineAuditLog;
import io.coderyeah.pet.domain.PetType;
import io.coderyeah.pet.dto.PetDTO;
import io.coderyeah.pet.mapper.PetDetailMapper;
import io.coderyeah.pet.mapper.PetMapper;
import io.coderyeah.pet.mapper.PetOnlineAuditLogMapper;
import io.coderyeah.pet.mapper.PetTypeMapper;
import io.coderyeah.pet.service.PetService;
import io.coderyeah.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author coderyeah
 * @description 针对表【t_pet】的数据库操作Service实现
 * @createDate 2022-09-23 20:07:49
 */
@Service
@Transactional
public class PetServiceImpl extends ServiceImpl<PetMapper, Pet> implements PetService {
    @Resource
    private PetMapper petMapper;
    @Autowired
    private PetTypeMapper typeMapper;
    @Autowired
    private PetDetailMapper detailMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper logMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Result getAllPets(PetDTO petDTO) {
        List<Pet> pets = petMapper.getAllPets(petDTO);
        Long totals = petMapper.getCounts(petDTO);
        return Result.success(pets, totals);
    }

    @Override
    public Result editType(PetDTO petDTO) {
        petDTO.setState(petDTO.getState() == 1 ? 0 : 1);
        petMapper.editType(petDTO);
        return Result.success(null);
    }

    @Override
    public Result getPetTypeTree() {
        // 查询所有类型
        final List<PetType> petTypes = typeMapper.selectList(new LambdaQueryWrapper<>());
        final ArrayList<PetType> types = new ArrayList<>();
        for (PetType petType : petTypes) {
            if (null != petType.getParentId()) {
                // 父级id不为空表示为子级类型
                // 将所有类型转换为map结构
                final Map<Long, PetType> map = petTypes.stream().collect(Collectors.toMap(PetType::getId, e -> e));
                // 找到父级类型
                final PetType type = map.get(petType.getParentId());
                type.getChildren().add(petType);
            } else {
                // 父级类型
                types.add(petType);
            }
        }
        return Result.success(types);
    }

    @Override
    public void saveOrModify(Pet pet) {
        if (null != pet.getId()) {
            // 修改
            petMapper.updateById(pet);
            final PetDetail detail = pet.getPetDetail();
            detailMapper.update(detail, new LambdaQueryWrapper<PetDetail>().eq(PetDetail::getPetId, pet.getId()));
        } else {
            // 添加宠物
            petMapper.insert(pet);
            // 添加详情
            final PetDetail petDetail = pet.getPetDetail();
            if (null != petDetail) {
                petDetail.setPetId(pet.getId());
                detailMapper.insert(petDetail);
            }
        }
    }

    @Override
    public Result onsale(List<Long> ids) {
        // 上架
        for (int i = 0; i < ids.size(); i++) {
            Pet pet = petMapper.selectById(ids.get(i));
            // 1.对上架的记录进行文本审核
            final Boolean bool1 = baiduAiAuditService.textAudit(pet.getName());
            final Boolean bool2 = baiduAiAuditService.imageAudit(pet.getResources());
            final PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(pet.getId());
            // 添加当前操作人
            final LoginInfo loginInfo = LoginInfoHolder.get();
            final Employee employee = employeeMapper.selectOne(new LambdaQueryWrapper<Employee>().eq(Employee::getLogininfoId, loginInfo.getId()));
            log.setAuditId(employee.getId());
            if (bool1 && bool2) {
                // 审核成功修改状态
                petMapper.onsale(ids.get(i), new Date());
                // 添加审核日志
                log.setState(1);
                log.setAuditTime(new Date());
                log.setNote("审核成功");
                logMapper.insert(log);
            } else {
                // 审核失败
                log.setState(0);
                log.setAuditTime(new Date());
                log.setNote("审核失败");
                logMapper.insert(log);
                return Result.fail("审核失败");
            }
        }
        return Result.success("审核成功");
    }

    @Override
    public Result offsale(List<Long> ids) {
        // 批量下架
        petMapper.offsale(ids, new Date());
        return Result.success(null);
    }

    @Override
    public Result getPetById(Long id) {
        Pet pet = petMapper.getPetById(id);
        return Result.success(pet);
    }
}




